import 'dart:math';

import 'package:flutter/material.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/routers/jump_router.dart';
import 'package:xvideos/tools_base/widget/net_image_widget.dart';

import '../../hj_model/cartoon_media_info.dart';

//acg video item组件
class AcgItemWidget extends StatefulWidget {
  final CartoonMediaInfo? info;
  final bool coverV; //竖屏封面
  final bool? heroUI; //是否需要hero动画
  final bool? showSymbol; //是否显示角标
  final Function()? tapCallback;

  const AcgItemWidget({
    Key? key,
    this.info,
    this.coverV = true,
    this.tapCallback,
    this.heroUI = false,
    this.showSymbol = true,
  });

  @override
  State<AcgItemWidget> createState() => _AcgItemWidgetState();
}

class _AcgItemWidgetState extends State<AcgItemWidget> {
  String? heroTag;

  @override
  void initState() {
    if (widget.heroUI == true) {
      heroTag = Random().nextInt(10000).toString();
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: widget.tapCallback ?? () => pushToCartoonPage(widget.info),
      child: Column(
        children: [
          Expanded(
              child: Stack(
            children: [
              LayoutBuilder(
                builder: (BuildContext context, BoxConstraints constraints) {
                  return NetworkImageLoader(
                    imageUrl: widget.coverV ? widget.info?.coverV ?? '' : widget.info?.coverH ?? '',
                    width: constraints.maxWidth,
                    height: constraints.maxHeight,
                    fit: BoxFit.cover,
                  );
                },
              ),
              if (widget.info?.isAudiobooks == true)
                Positioned(
                  left: 8,
                  top: 8,
                  height: 24,
                  width: 24,
                  child: Image.asset('voice_noval_icon.png'.videoPath),
                )
            ],
          )),
          6.sizeBoxH,
          Row(
            children: [
              Expanded(
                child: Text(
                  widget.info?.title ?? '',
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.white.withOpacity(.9),
                    fontWeight: FontWeight.w500,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  textAlign: TextAlign.left,
                ),
              )
            ],
          ),
          4.sizeBoxH,
          Row(
            children: [
              Expanded(
                child: Text(
                  '${widget.info?.episodeNumberStatus ?? ''} · ${widget.info?.getUpdateString}',
                  textAlign: TextAlign.left,
                  style: TextStyle(fontSize: 11, color: _statusColor, fontWeight: FontWeight.w400),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  Color get _statusColor {
    return widget.info?.updateStatus == 1 ? Color(0xffEEC76B) : Colors.white.withOpacity(.45);
  }
}
